package com.ctshk.rpc.bus.service.impl;

import java.math.BigDecimal;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.ServiceChargeType;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.BConst;
import com.ctshk.rpc.bus.dto.BusProductReservationSettingsDTO;
import com.ctshk.rpc.bus.entity.BusProductReservationSettings;
import com.ctshk.rpc.bus.mapper.BusProductMapper;
import com.ctshk.rpc.bus.mapper.BusProductReservationSettingsMapper;
import com.ctshk.rpc.bus.service.IBusProductReservationSettingsService;

/**
 * <p>
 * 巴士产品预订设置 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-02-19
 */
@DubboService
public class BusProductReservationSettingsServiceImpl extends ServiceImpl<BusProductReservationSettingsMapper, BusProductReservationSettings> implements IBusProductReservationSettingsService, BaseService {
	
	@Autowired
	BusProductReservationSettingsMapper busProductReservationSettingsMapper;
	
	@Autowired
	BusProductMapper busProductMapper;

	/**
	 * 查询某产品的预订设置
	 * @param productId
	 * @return
	 */
	@Override
	public Result<BusProductReservationSettingsDTO> findReservationSettingsByProduct(Long productId) {
		validateNotNull(productId);
		BusProductReservationSettingsDTO dto = busProductReservationSettingsMapper.selectOneByKey(BusProductReservationSettingsDTO.class, "bus_product_id", productId);
		return newResult(dto);
	}
	
	/**
	 * 计算退改手续费
	 * @param productId
	 * @param amount
	 * @return
	 */
	@Override
	public BigDecimal calcRefundFee(Long productId, BigDecimal amount) {
		validateNotNull(productId, amount);
		
		BusProductReservationSettingsDTO dto = busProductReservationSettingsMapper.selectOneByKey(BusProductReservationSettingsDTO.class, "bus_product_id", productId);
		if (dto == null || dto.getRefundFeeType() == null || dto.getRefundFeePrice() == null) {
			return amount;
		}

		//固定数值
		if (ServiceChargeType.FIXED.getCode().equals(dto.getRefundFeeType())) {
			amount = amount.subtract(dto.getRefundFeePrice());
		} else if (ServiceChargeType.FIXED.getCode().equals(dto.getRefundFeeType())) {  //百分比
			amount = amount.subtract(amount.multiply(dto.getRefundFeePrice()).divide(BConst.HUNDRED_BD));
		}
		
		
		
		if (Assist.ltZero(amount)) {
			amount = BigDecimal.ZERO;
		}
		
		return amount;
	}

}
